<template>
  <transition name='slide'>
    <music-list :songs='songs' :title='title' :bg-image='bgImage'></music-list>
  </transition>
</template>

<script type="text/ecmascript-6">
import {mapGetters} from 'vuex'
import {getSingerDetail} from 'api/singer'
import {createSong} from 'common/js/song'
import MusicList from 'components/music-list/music-list'
export default {
  data() {
    return {
      songs: []
    }
  },
  components: {
    MusicList
  },
  computed: {
    title() {
      return this.singer.name
    },
    bgImage() {
      return this.singer.img1v1Url
    },
    ...mapGetters([
      'singer'
    ])
  },
  created() {
    this._getDetail()
  },
  methods: {
    // 根据vuex传递过来的歌手ID进入歌手详情
    _getDetail() {
      // 如果没有歌手id,则回退到所以歌手列表页面
      if (!this.singer.id) {
        this.$router.push('/singer')
        return
      }
      getSingerDetail(this.singer.id).then((res) => {
        if (res.code === 200) {
          this.songs = this._normalSongs(res.data)
        }
      })
    },
    // 重新处理list，将歌曲相关信息存放入song对象中，最后push成json
    _normalSongs(list) {
      let ret = []
      list.forEach((item) => {
        if (item.id) {
          ret.push(createSong(item))
        }
      })
      return ret
    }
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  .slide-enter-active, .slide-leave-active
    transition: all 0.3s
  .slide-enter, .slide-leave-to
    transform: translate3d(100%, 0, 0)
</style>